package OtherPubs;

import Rout.*;
import static Rout.AggDetect.agentDetect;
import com.sun.org.omg.SendingContext.CodeBasePackage.URLSeqHelper;
import java.awt.RenderingHints;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import javax.script.ScriptException;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;

public class Catcher {

    private GoBefore browser = new GoBefore();
    private ArrayList<String> urls = new ArrayList<String>();
    private JavascriptExecutor js = (JavascriptExecutor) browser.getDriver();
    private int attempt = 100;   // how much we'll try for catch events

    @BeforeMethod
    public void setUp() throws Exception {
        urls.add("http://m.watchcartoononline.com/mobile-suit-gundam-00-season-2-episode-25-english-dubbed");


        System.out.println("\n Run test: " + getClass().getSimpleName());
        browser.setXpFilename(null);
//        browser.setRepDir(getClass().getSimpleName());
        browser.setEnableSafeBrow(Boolean.FALSE);
//        browser.setChDrvPath("/home/user/Dropbox/WebDriver/lin64/chromedriver");
        browser.setRemoteDriver("192.168.4.170"); //Win7Clone VBox
        browser.setCssButton("#footerCpmIn");
        browser.setXpButton("//*[@id=\"footerCpmIn\"]");
        browser.setUserAg(3);

    }

    public String getReferal(WebDriver driver, String url) throws ScriptException {
        JavascriptExecutor js;
        js = (JavascriptExecutor) driver;
        String refURL = (String) js.executeScript("return document.referrer");
//        System.out.println("REF: " + refURL);
        return refURL;
    }

    public void closeAlerts() throws Exception {
        try {
            browser.getDriver().switchTo().alert().accept();
        } catch (Exception e) {
        }

    }

    @Test //Void Footer Baner
    public void Test() throws Exception {

        for (String url : urls) {

            browser.setCurURL(url);
            System.out.println("\n\nTest case: " + (urls.indexOf(url) + 1));

            
           
//            browser.maximazeBrowser();
            
           
            browser.setRepDir("test_" + (urls.indexOf(url) + 1) + "_" + browser.detectGEO());
           

            
            for (int m = 0; m < attempt; m++) {
                
                browser.setUp();
                System.out.println("\nTest URL is: " + url);
                System.out.println("attempt :" + m);
                System.out.println("Click on button");
                Thread.sleep(6000);
               try {
                 browser.testRun();
            } catch (Exception e) {
                e.printStackTrace();
            }
                Thread.sleep(6000);
                browser.closaAlert();
                
                
                
//Get number of open tabs            
                ArrayList<String> newTab = new ArrayList<String>(browser.getDriver().getWindowHandles());
                if (newTab.size() > 1) {
                    System.out.print("Open new tab.");
                    Thread.sleep(5000);
                    System.out.println(" Total open tabs is: " + newTab.size());
                    for (int i = 1; i < newTab.size(); i++) {
//If 2 tabs present, switch to new tab  

                        try {
                            browser.getDriver().switchTo().window(newTab.get(i));
                            System.out.println("Switch to tab # " + i);
                            closeAlerts();
                            getReferal(browser.getDriver(), browser.getDriver().getCurrentUrl());

                            closeAlerts();
                            Thread.sleep(1000);

                            String NextUrl = browser.getDriver().getCurrentUrl();
                            Thread.sleep(3000);
                           
                            
                            if (NextUrl.contains("king.com")||browser.getCurURL().contains("kings.com")) {
                             browser.takeFoto();
                            System.out.println("catch!!");
                            System.out.println("Redirect to: " + NextUrl);
                            System.out.println("Redirect via URL: " + getReferal(browser.getDriver(), NextUrl));
                            System.out.println("!" + browser.getFotoName() + "|thumbnail!");
                            browser.Fail();
                            }
                            
                            
                            

                        } catch (Exception e) {
                            System.out.println("exeption in  loop" + e);
                        }

                    }

                } else {
                    System.out.println("No popunders");
                }
                            closeAlerts();
            browser.getDriver().quit();
            Thread.sleep(1000);
            }

        }
//    }
//        browser.getDriver().close();
    }

    @AfterMethod
    public void Down() throws Exception {
        browser.getDriver().quit();

    }

}
